package com.boboPlanet.comm.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO 年龄转化工具类
 * @Author: mis_wu
 * @Date: 2023/3/1
 **/
@Slf4j
public class AgeConvertUtil {

    private static final String AGE_UNIT_YEAR = "岁";
    private static final String AGE_UNIT_MONTH = "月";
    private static final String AGE_UNIT_WEEK = "周";
    private static final String AGE_UNIT_DAY = "天";
    private static final String AGE_UNIT_HOUR = "时";
    private static final String AGE_UNIT_MINUTES = "分";

    private static final String AGE_UNIT_GE = "个";

    private static final List<String> ALLOW_STR =
            Arrays.asList("岁", "月", "周", "天", "时", "分", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

    private static final List<String> ALLOW_AGE_UNIT = Arrays.asList("岁", "月", "周", "天", "时", "分");

    private static final String[] EMPTY_STRING_ARR = new String[0];

    // 年龄转换
    public static long ageToHour(String age) {
        return ageToHour2(filter(age));
    }

    private static String filter(String age) {
        if (StringUtils.isBlank(age)) {
            return StringUtils.EMPTY;
        }
        age = StringUtils.trim(age);
        StringBuilder builder = new StringBuilder();
        char[] chars = age.toCharArray();
        for (char ch : chars) {
            String str = String.valueOf(ch);
            if (ALLOW_STR.contains(str)) {
                builder.append(str);
            }
        }
        return builder.toString();
    }

    private static long ageToHour2(String age) {
        age = StringUtils.trim(age);

        long hours = 0;
        String[] ageArr;

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_YEAR)) {
            ageArr = age.split(AGE_UNIT_YEAR);
            hours += yearToHour(convertToLong(ageArr[0]));
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_MONTH)) {
            ageArr = age.split(AGE_UNIT_MONTH);
            hours += monthToHour(convertToLong(ageArr[0]));
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_WEEK)) {
            ageArr = age.split(AGE_UNIT_WEEK);
            hours += weekToHour(convertToLong(ageArr[0]));
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_DAY)) {
            ageArr = age.split(AGE_UNIT_DAY);
            hours += dayToHour(convertToLong(ageArr[0]));
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_HOUR)) {
            hours += timeToHour(age);
        }

        return hours;
    }

    private static long yearToHour(long year) {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.YEAR, (int) ((long) calendar.get(Calendar.YEAR) - year));
        long interval = currentDate.getTime() - calendar.getTime().getTime();
        return TimeUnit.HOURS.convert(interval, TimeUnit.MILLISECONDS);
    }

    private static long monthToHour(long month) {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.MONTH, (int) ((long) calendar.get(Calendar.MONTH) - month));
        long interval = currentDate.getTime() - calendar.getTime().getTime();
        return TimeUnit.HOURS.convert(interval, TimeUnit.MILLISECONDS);
    }

    private static long weekToHour(long week) {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.WEEK_OF_MONTH, (int) ((long) calendar.get(Calendar.WEEK_OF_MONTH) - week));
        long interval = currentDate.getTime() - calendar.getTime().getTime();
        return TimeUnit.HOURS.convert(interval, TimeUnit.MILLISECONDS);
    }

    private static long dayToHour(long days) {
        return TimeUnit.HOURS.convert(days, TimeUnit.DAYS);
    }

    private static long timeToHour(String time) {
        if (StringUtils.isBlank(time)) {
            return 0;
        }
        time = StringUtils.trim(time);
        if (!time.contains(AGE_UNIT_HOUR) || time.startsWith(AGE_UNIT_HOUR)) {
            return 0;
        }
        return convertToLong(time.split(AGE_UNIT_HOUR)[0]);
    }

    private static long convertToLong(String text) {
        try {
            if (StringUtils.isNotBlank(text)) {
                text = StringUtils.trim(text);
            }
            if (StringUtils.isNumeric(text)) {
                return Long.parseLong(text);
            }
        } catch (Exception e) {
            log.error(text + " :无法解析的时间日期文本: " + e.getMessage(), e);
        }
        throw new RuntimeException(text + " :无法解析的时间日期文本");
    }


    /**
     * 年龄转换小时，年：365  月：31 周：7
     * @param age String
     * @return
     * 将 2个月 这种的 个 替换
     */
    public static long ageToHour3(String age) {
        age = StringUtils.trim(age);

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_GE)){
            age = age.replace(AGE_UNIT_GE,"");
        }

        long hours = 0;
        String[] ageArr;

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_YEAR)) {
            ageArr = age.split(AGE_UNIT_YEAR);
            hours += convertToLong(ageArr[0]) * 365 * 24;
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_MONTH)) {
            ageArr = age.split(AGE_UNIT_MONTH);
            hours += convertToLong(ageArr[0]) * 31 * 24;
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_WEEK)) {
            ageArr = age.split(AGE_UNIT_WEEK);
            hours += convertToLong(ageArr[0]) * 7 * 24;
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_DAY)) {
            ageArr = age.split(AGE_UNIT_DAY);
            hours += convertToLong(ageArr[0]) * 24;
            age = ageArr.length > 1 ? ageArr[1] : null;
        }

        if (StringUtils.isNotBlank(age) && age.contains(AGE_UNIT_HOUR)) {
            hours += convertToLong(age);
        }

        return hours;
    }


}
